package com.tgy.algorithm.base._数组和;


/**
 *  [1,2,3,4,5,6]
 *
 */
public class _正整数数组和 {


    public static int valSum(int[] nums,int k) {

        if (nums == null || nums.length == 0 || k <= 0) {
            return 0;
        }

        int len = nums.length;
        int leftIndex = 0;
        int rightIndex = 0;
        int sum = nums[0];

        int maxLen = 0;
        while (rightIndex < len) {
            if (sum == k) {
                maxLen = Math.max(maxLen,rightIndex - leftIndex + 1);
                rightIndex++;
                if (rightIndex >= len) {
                    break;
                }
                sum += nums[rightIndex];
            }else if (sum < k) {
                rightIndex++;
                if (rightIndex >= len) {
                    break;
                }
                sum += nums[rightIndex];
            }else {
                sum -= nums[leftIndex++];
            }
        }

        return maxLen;
    }


    public static void main(String[] args) {
//        int[] values = new int[]{3,2,1,4,7};
        int[] values = new int[]{9,2,1,1,1,1,1};

        int valSum = valSum(values, 7);
        System.out.println(valSum);
    }
}
